/**
 * 
 */
package com.jm.web.builder.form;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.form.baseitem.SelectItem;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * 2011/10/6 上午9:01:38
 */
public class SelectBuilder extends BaseBuilder{
	protected SelectItem select = null ;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		select = new SelectItem();
		select.InitTag(tag);
		select.setFromTag(true);//设置此控件是tag生成的
		select.init();
		
		//设置select 的值
		String strValue = select.getValue();
		if(StringHelper.IsNullOrEmpty(strValue) && this.getBaseTag().getDataSourceTag()!=null){
			if(this.getBaseTag().getDataSourceTag().getDataSource().ContainesParam(select.getName()))
			{
				if(this.getBaseTag().getDataSourceTag().getDataSource().gpsvs(select.getName()).length == 1)
				{
					//处理表单项名称唯一
					strValue = this.getBaseTag().getDataSourceTag().getDataSource().GetParamStringValue(select.getName(), "");
				}
				else if(this.getBaseTag().getDataSourceTag().getDataSource().gpsvs(select.getName()).length > 1)
				{
					//处理表单项名称重复的item
					String args[] = this.getBaseTag().getDataSourceTag().getDataSource().GetParamStringValues(select.getName(), null);
					if(args != null && args.length > select.getIndex()){
						strValue = args[select.getIndex()];
					}
				}
			}
		}
		select.setValue(strValue);
	}
	
	@Override
	public void RenderEnd() {
		//处理slelect 连跳功能
		if(select.IsContainsKey("config")){
			String strConfig = StringHelper.Format("{%1$s}",select.GetExtValue("config","").toLowerCase());
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("var config_%2$s = %1$s;",strConfig,select.getId());
			sb.Append("var ajax_jump_%1$s = function(args){",select.getId());
			sb.Append("var id = '%1$s';",select.getId());
			sb.Append("var _val = $(this).val();if(args && args['value'])_val=args['value'];");
			sb.Append("$P.processoptions(id,null,config_%1$s);",select.getId());
			sb.Append("if($P.isnullorempty(_val)){$('#'+id).trigger('change');return;}");
			
			sb.Append("var p = {};");
			sb.Append("p['action']='ajax_getOptions';");
			sb.Append("p['params']='parentvalue';");
			sb.Append("p['parentvalue']=_val;");
			sb.Append("$.extend(p,config_%1$s);",select.getId());
			sb.Append("if((typeof p['paramsfun']) == 'function'){p['paramsfun'].call(null,p);delete p['paramsfun'];}");
			
			sb.Append("var doaction = function(jo){");
				sb.Append("if(jo != undefined && jo.ret == 1 && jo.data){");
				sb.Append("$P.processoptions(id,jo.data['options'],config_%1$s);$('#'+id).trigger('change');",select.getId());
				sb.Append("};");
			sb.Append("};");
			sb.Append("$P.ajax({data : p,succ :doaction,url:'"+this.getBaseTag().getPage().getContextPath()+"/include/commonajax_backend.jsp'});");
			sb.Append("};");
			
			sb.Append("var xtype = config_%1$s['xtype'];",select.getId());
			sb.Append("var parentid = config_%1$s.parentid;",select.getId());
			sb.Append("var parentel = $('#'+parentid);");
			sb.Append("if(parentel.gv('xtype') != '') xtype=parentel.gv('xtype');");
			
			sb.Append("if('org' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",select.getId());
			sb.Append("}else if('combobox' == xtype || 'combobox_text' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",select.getId());
				sb.Append("parentel.bind('change',function(){if($(this).val().trim() == ''){$('#'+config_%1$s.parentid).trigger('picked',this);}});",select.getId());
			sb.Append("}else if('multiselect' == xtype){");
				sb.Append("parentel.bind('picked',ajax_jump_%1$s);",select.getId());
			sb.Append("}else if('text' == xtype){");
				sb.Append("parentel.blur(ajax_jump_%1$s);",select.getId());
			sb.Append("}else{");
				sb.Append("parentel.change(ajax_jump_%1$s);",select.getId());
			sb.Append("}");
			
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
		}
		
		select.RemoveExtValue("config");
		select.onRender2(this.getOut());
	}
}
